<?php

/**
 * This is the model class for table "curso".
 *
 * The followings are the available columns in table 'curso':
 * @property integer $id_curso
 * @property integer $id_clase
 * @property integer $id_carrera
 * @property string $semestre
 *
 * The followings are the available model relations:
 * @property ProfesorXCurso[] $profesorXCursos
 * @property Clase $idClase
 * @property Carrera $idCarrera
 *
 * Propiedades adicionales
 * @property integer $id_facultad
 */
class Curso extends CActiveRecord
{
	public $id_facultad;
	public $id_carrera;
	public $id_materia;
	public $id_profesor;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Curso the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'curso';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_curso, semestre, id_materia, seccion', 'required'),
			array('id_carrera, id_facultad', 'required'),
			//array('id_carrera', 'numerical', 'integerOnly'=>true),
			array('semestre', 'length', 'max'=>4),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_curso, id_carrera, semestre', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'profesorXCursos' => array(self::HAS_MANY, 'ProfesorXCurso', 'id_curso'),
			'idClase' => array(self::BELONGS_TO, 'Clase', 'id_clase'),
			'idCarrera' => array(self::BELONGS_TO, 'Carrera', 'id_carrera'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_facultad' => 'Facultad',
			'id_carrera' => 'Carrera',
			'id_curso' => 'Id de Curso',
			'id_materia' => 'Id de Materia',
			//'semestre' => 'Semestre',
			'seccion' => 'Seccion',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_curso',$this->id_curso);
		$criteria->compare('id_carrera',$this->id_carrera);
		$criteria->compare('semestre',$this->semestre,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function facultad()
	{
		$facultad = Yii::app()->db->createCommand()
		->select('id_facultad, nombre')
		->from('facultad')
		->queryAll();
		$facu = array();
		foreach ($facultad as $f) {
			$facu[$f['id_facultad']] = $f['nombre'];
		}
		return $facu;
	}
	
	public function carrera($id_facultad)
	{
		
		$carreraString="select id_carrera, nombre from carrera where id_facultad='".$id_facultad."';";
		$carreraQuery = Yii::app()->db->createCommand($carreraString)->queryAll();
		$carre=array();
		foreach ($carreraQuery as $c) {
			$carre[$c['id_carrera']] = $c['nombre'];
		}
		return $carre;
	}
	
		public function materia($id_carrera)
	{
		
		$materiaString="select id_materia, nombre from materia where id_carrera='".$id_carrera."';";
		$materiaQuery = Yii::app()->db->createCommand($materiaString)->queryAll();
		$mate=array();
		foreach ($materiaQuery as $c) {
			$mate[$c['id_materia']] = $c['nombre'];
		}
		return $mate;
	}
	
	
	public function profesor()
	{
		$profesorString="select id_profesor, nombres, apellidos 
						from usuario u join profesor p 
						on u.ci=p.ci order by apellidos;";
		$profesorQuery = Yii::app()->db->createCommand($profesorString)->queryAll();
		$profe=array();
		foreach ($profesorQuery as $c) {
			$profe[$c['id_profesor']] = $c['apellidos'].', '.$c['nombres'];
		}
		return $profe;
	}
	
	public function nombreMateria($id_materia)
	{
		$materiaString="select nombre
						from materia 
						where id_materia='".$id_materia."';";
		$materiaQuery = Yii::app()->db->createCommand($materiaString)->queryAll();
		foreach ($materiaQuery as $m){
			$materia=$m['nombre'];
		}
		return $materia;
	
	
	
	}
	
	
}